﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Drawing;

namespace ConfExpert.Forms.Depo.Kauf
{
    public partial class OrtakKumasRaporu : DevReport
    {
        public OrtakKumasRaporu()
        {
            InitializeComponent();
        }
        public OrtakKumasRaporu(string Article, string Rolls, string Idx, string Atolye, int CustomerId, string SpName,bool IsWaterMark)
            : this()
        {
            Watermark.Text = IsWaterMark ? "Önizleme" : "";
            Watermark.ShowBehind = false;

            MyData1.Merge(ExecuteDataSet(SpName, Article, Rolls, Idx, CustomerId));
            MyData1.xOrtakKumasMaster.Merge(MyData1.Tables["Table"]);
            MyData1.xOrtakKumasDetail.Merge(MyData1.Tables["Table1"]);

            xrSubOrtakKumas sr2 = (xrSubOrtakKumas)xrDosya.ReportSource;
            sr2.fillTables(MyData1.xOrtakKumasDetail);

            string Customer = MyData1.Tables["Table2"].Rows[0]["Name"].ToString();
            string DosyaNo = MyData1.Tables["Table2"].Rows[0]["DosyaNo"].ToString();
            xrLabel1.Text = Customer + " ORTAK KUMAŞ RAPORU ";
            xrTableCell19.Text = DosyaNo;
            tcTarih.Text = DateTime.Now.ToString("dd.MM.yyyy");
            tcAtolye.Text = Atolye;
            this.PrintingSystem.EndPrint += PrintingSystem_EndPrint;
        }

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        {
            this.ClosePreview();
        }

        private void xrDosya_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ((xrSubOrtakKumas)((XRSubreport)sender).ReportSource).PrdId2.Value =
       Convert.ToInt32(GetCurrentColumnValue("ProductId"));
        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            Detail.HeightF = 335F;
            tbFoy.HeightF = 335F;
        }

        private void xrTable2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            tbFoy.HeightF = 335F;
            Detail.HeightF = 335F;
            trFoy.HeightF = 268;
            trNot.HeightF = 67;
            int a = MyData1.xOrtakKumasDetail.Select("ProductId=" + GetCurrentColumnValue("ProductId").ToString()).Length;
            if (a > 4)
            {
                tbFoy.HeightF = a * 67 + 67;
                trFoy.HeightF = a * 67;
                trNot.HeightF = 67;
                //Detail.HeightF = tbFoy.HeightF + 67;
            }
        }


        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 9.5f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.Width / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        private void tcNot_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }
    }
}
